package com.wunu.smartlock1.adapter;

import android.app.Dialog;
import android.view.Gravity;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.wunu.smartlock1.activity.QrcodeActivity;
import com.wunu.smartlock1.model.AuthorizedMemberVo;
import java.util.List;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.util.ImageLoaderPicture;

/**
 * Created by admin on 2018/4/22.
 */

public class AuthorizedMemberAdapter extends ArrayAdapter  implements View.OnClickListener {
    private int resourceId;
    View inflate;
    Dialog dialog;
    TextView resetlock;//启用
    TextView stoplock;//删除
    TextView cancel;//取消
    private RelativeLayout lineLayout;//每行所在的线性布局

    Context context;
    public AuthorizedMemberAdapter(Context context, int textViewResourceId,List<AuthorizedMemberVo> list){
        super(context, textViewResourceId, list);
        this.context = context;
        resourceId = textViewResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AuthorizedMemberVo memberVo = (AuthorizedMemberVo) getItem(position); // 获取当前项的Fruit实例
        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象 使用Inflater对象来将布局文件解析成一个View
        TextView authorizedType = (TextView) view.findViewById(R.id.authorizedType);//获取该布局内的文本视图
        TextView authorizedName = (TextView) view.findViewById(R.id.authorizedName);//获取该布局内的文本视图
        TextView authorizedDescript = (TextView) view.findViewById(R.id.authorizedDescript);//获取该布局内的文本视图
        TextView userContent = (TextView) view.findViewById(R.id.userContent);//获取该布局内的文本视图
        TextView usedTimes = (TextView) view.findViewById(R.id.usedTimes);//获取该布局内的文本视图
        TextView leftTimes = (TextView) view.findViewById(R.id.leftTimes);//获取该布局内的文本视图
        ImageView authorizedPicUrl = (ImageView) view.findViewById(R.id.authorizedPicUrl);//获取该布局内的图片视图authorizedPicUrl
        ImageView menberPicUrl = (ImageView) view.findViewById(R.id.menberPicUrl);//获取该布局内的图片视图


        userContent.setText(memberVo.getUserContent());//为文本视图设置文本内容
        authorizedType.setText(memberVo.getAuthorizedType());//为文本视图设置文本内容
        authorizedName.setText(memberVo.getAuthorizedName());//为文本视图设置文本内容
        authorizedDescript.setText(memberVo.getAuthorizedDescript());//为文本视图设置文本内容

        ImageLoader.getInstance().displayImage(memberVo.getAuthorizedPicUrl(), authorizedPicUrl,new ImageLoaderPicture(context).getOptions(),new SimpleImageLoadingListener());

        //认证成员
        if(0 == memberVo.getMemberType()){
            menberPicUrl.setImageResource(memberVo.getMenberPicUrl());//为图片视图设置图片资源

            //为该行的linearLayout添加点击事件
            lineLayout = (RelativeLayout) view.findViewById(R.id.lineLayout);
            lineLayout.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                        memberShow();
                    }
                });
        }else{//临时秘钥
            usedTimes.setText(""+memberVo.getUsedTimes());//为文本视图设置文本内容
            leftTimes.setText("余"+memberVo.getLeftTimes()+"次");//为文本视图设置文本内容
        }
        return view;
    }

    public void memberShow(){
        dialog = new Dialog(context,R.style.smartlock_ActionSheetDialogStyle);
        inflate = LayoutInflater.from(context).inflate(R.layout.smartlock_activity_authorized_item_option_dialog, null);
        resetlock = inflate.findViewById(R.id.resetlock);//取消弹框
        stoplock = inflate.findViewById(R.id.stoplock);//取消弹框
        cancel = inflate.findViewById(R.id.cancel);//取消弹框
        resetlock.setOnClickListener(this);
        cancel.setOnClickListener(this);
        stoplock.setOnClickListener(this);
        dialog.setContentView(inflate);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity( Gravity.BOTTOM);
        dialog.show();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.cancel){
            dialog.dismiss();
        }else if(id == R.id.resetlock){
            Toast.makeText(context,"点击了启用按钮",Toast.LENGTH_SHORT).show();
        }else if(id == R.id.stoplock){
            Toast.makeText(context,"点击了删除按钮",Toast.LENGTH_SHORT).show();
        }
        //dialog.dismiss();
    }
}
